package com.jbinfo.qingcaowan.studyManager.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jbinfo.common.utils.DataTables;
import com.jbinfo.common.utils.StringUtils;
import com.jbinfo.common.utils.UUIDUtils;
import com.jbinfo.qingcaowan.common.BusinessException;
import com.jbinfo.qingcaowan.common.CheckParamUtil;
import com.jbinfo.qingcaowan.common.ExceptionConstants.BusinessExceptionCode;
import com.jbinfo.qingcaowan.studyManager.dao.BookManagerDao;
import com.jbinfo.qingcaowan.studyManager.dto.BookInfoModel;
import com.jbinfo.qingcaowan.studyManager.entity.BookInfo;
import com.jbinfo.qingcaowan.studyManager.service.BookManagerService;
/**
 * 
* @ClassName: UserServiceImpl 
* @Description: TODO 用户管理service实现类
* @author ZhangLei 
* @date 2016年6月22日 上午10:23:02
 */
@Service("bookManagerService")
public class BookManagerServiceImpl implements BookManagerService {

	@Resource(name="bookManagerDao")
	private BookManagerDao bookManagerDao;

	/**书籍管理**********************************************************/
	@Override
	public DataTables<BookInfo> findByParam(BookInfoModel bookInfoModel) {
		String today = bookInfoModel.getShelvesTime();
		if(!(today==null||"".equals(today))){
			bookInfoModel.setShelvesTime1(today+" 00:00:00");
			bookInfoModel.setShelvesTime2(today+" 23:59:59");
		}
		List<BookInfo> tList = bookManagerDao.findByParam(bookInfoModel);
		return DataTables.combinTotalRecords(tList);
	}

	@Override
	public void save(BookInfo bookInfo) {
		//处理书籍编号
		String bookId = UUIDUtils.create();
		bookInfo.setBookId(bookId);
		//所有的英文逗号换成中文逗号
		bookInfo.setAuthors(bookInfo.getAuthors().replace(",", "，"));
		bookInfo.setPress(bookInfo.getPress().replace(",", "，"));
		
		bookManagerDao.save(bookInfo);
		
	}

	@Override
	public BookInfo getBookInfo(String bookId) {
		return bookManagerDao.getBookInfo(bookId);
	}

	@Override
	public int editBook(BookInfo bookInfo) {
		//所有的英文逗号换成中文逗号
		bookInfo.setAuthors(bookInfo.getAuthors().replace(",", "，"));
		bookInfo.setPress(bookInfo.getPress().replace(",", "，"));
		return bookManagerDao.editBook(bookInfo);
	}

	@Override
	public int delBook(String bookId) {
		return bookManagerDao.delBook(bookId);
	}

	@Override
	public List<BookInfo> queryBookList(BookInfo bookInfo) {
		return bookManagerDao.queryBookList(bookInfo);
	}

	@Override
	public List<BookInfo> getBookByCode(BookInfo bookInfo) {
		return bookManagerDao.getBookByCode(bookInfo);
	}

	// 获取书籍阅读概要信息
	@Override
	public List<BookInfo> getBookGist(String orgId) {
		if (orgId == null || StringUtils.isNotBlankNumeric(orgId)) {
			throw new BusinessException(BusinessExceptionCode.param_error, "组织ID不能为空");
		}
		List<BookInfo> bookList = bookManagerDao.selectBookInfoByOrg(orgId);
		return bookList;
	}
	
	// 获取书籍下卡片总数
	public Object getCardCnt(BookInfo bookInfo) {
		
		CheckParamUtil.checkFieldIfNotBlank(bookInfo, "bookId");
		
		String strCardCnt = "";
		
		Integer nCardCnt = bookManagerDao.selectCardCntByParam(bookInfo);
		strCardCnt = nCardCnt.toString();
		
		return strCardCnt;
	}
}
